﻿namespace MDXStudio.Utils
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Sequential)]
    internal struct HSBColor
    {
        private float h;
        private float s;
        private float b;
        private int a;
        public HSBColor(float h, float s, float b)
        {
            this.a = 0xff;
            this.h = Math.Min(Math.Max(h, 0f), 255f);
            this.s = Math.Min(Math.Max(s, 0f), 255f);
            this.b = Math.Min(Math.Max(b, 0f), 255f);
        }

        public HSBColor(int a, float h, float s, float b)
        {
            this.a = a;
            this.h = Math.Min(Math.Max(h, 0f), 255f);
            this.s = Math.Min(Math.Max(s, 0f), 255f);
            this.b = Math.Min(Math.Max(b, 0f), 255f);
        }

        public HSBColor(System.Drawing.Color color)
        {
            HSBColor color2 = FromColor(color);
            this.a = color2.a;
            this.h = color2.h;
            this.s = color2.s;
            this.b = color2.b;
        }

        public float H
        {
            get
            {
                return this.h;
            }
        }
        public float S
        {
            get
            {
                return this.s;
            }
        }
        public float B
        {
            get
            {
                return this.b;
            }
        }
        public int A
        {
            get
            {
                return this.a;
            }
        }
        public System.Drawing.Color Color
        {
            get
            {
                return FromHSB(this);
            }
        }
        public static System.Drawing.Color ShiftHue(System.Drawing.Color c, float hueDelta)
        {
            HSBColor hsbColor = FromColor(c);
            hsbColor.h += hueDelta;
            hsbColor.h = Math.Min(Math.Max(hsbColor.h, 0f), 255f);
            return FromHSB(hsbColor);
        }

        public static System.Drawing.Color ShiftSaturation(System.Drawing.Color c, float saturationDelta)
        {
            HSBColor hsbColor = FromColor(c);
            hsbColor.s += saturationDelta;
            hsbColor.s = Math.Min(Math.Max(hsbColor.s, 0f), 255f);
            return FromHSB(hsbColor);
        }

        public static System.Drawing.Color ShiftBrighness(System.Drawing.Color c, float brightnessDelta)
        {
            HSBColor hsbColor = FromColor(c);
            hsbColor.b += brightnessDelta;
            hsbColor.b = Math.Min(Math.Max(hsbColor.b, 0f), 255f);
            return FromHSB(hsbColor);
        }

        public static System.Drawing.Color FromHSB(HSBColor hsbColor)
        {
            float b = hsbColor.b;
            float num2 = hsbColor.b;
            float num3 = hsbColor.b;
            if (hsbColor.s != 0f)
            {
                float num4 = hsbColor.b;
                float num5 = (hsbColor.b * hsbColor.s) / 255f;
                float num6 = hsbColor.b - num5;
                float num7 = (hsbColor.h * 360f) / 255f;
                if (num7 < 60f)
                {
                    b = num4;
                    num2 = ((num7 * num5) / 60f) + num6;
                    num3 = num6;
                }
                else if (num7 < 120f)
                {
                    b = ((-(num7 - 120f) * num5) / 60f) + num6;
                    num2 = num4;
                    num3 = num6;
                }
                else if (num7 < 180f)
                {
                    b = num6;
                    num2 = num4;
                    num3 = (((num7 - 120f) * num5) / 60f) + num6;
                }
                else if (num7 < 240f)
                {
                    b = num6;
                    num2 = ((-(num7 - 240f) * num5) / 60f) + num6;
                    num3 = num4;
                }
                else if (num7 < 300f)
                {
                    b = (((num7 - 240f) * num5) / 60f) + num6;
                    num2 = num6;
                    num3 = num4;
                }
                else if (num7 <= 360f)
                {
                    b = num4;
                    num2 = num6;
                    num3 = ((-(num7 - 360f) * num5) / 60f) + num6;
                }
                else
                {
                    b = 0f;
                    num2 = 0f;
                    num3 = 0f;
                }
            }
            return System.Drawing.Color.FromArgb(hsbColor.a, (int) Math.Round((double) Math.Min(Math.Max(b, 0f), 255f)), (int) Math.Round((double) Math.Min(Math.Max(num2, 0f), 255f)), (int) Math.Round((double) Math.Min(Math.Max(num3, 0f), 255f)));
        }

        public static HSBColor FromColor(System.Drawing.Color color)
        {
            HSBColor color2 = new HSBColor(0f, 0f, 0f);
            color2.a = color.A;
            float r = color.R;
            float g = color.G;
            float b = color.B;
            float num4 = Math.Max(r, Math.Max(g, b));
            if (num4 > 0f)
            {
                float num5 = Math.Min(r, Math.Min(g, b));
                float num6 = num4 - num5;
                if (num4 > num5)
                {
                    if (g == num4)
                    {
                        color2.h = (((b - r) / num6) * 60f) + 120f;
                    }
                    else if (b == num4)
                    {
                        color2.h = (((r - g) / num6) * 60f) + 240f;
                    }
                    else if (b > g)
                    {
                        color2.h = (((g - b) / num6) * 60f) + 360f;
                    }
                    else
                    {
                        color2.h = ((g - b) / num6) * 60f;
                    }
                    if (color2.h < 0f)
                    {
                        color2.h += 360f;
                    }
                }
                else
                {
                    color2.h = 0f;
                }
                color2.h *= 0.7083333f;
                color2.s = (num6 / num4) * 255f;
                color2.b = num4;
            }
            return color2;
        }
    }
}

